package com.hungtrung.entity;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.hungtrung.game.GameConfig;

public class GiaCam extends Entity {
	
	TrungChatThai trungChatThai;
	boolean timerOn = false;
	public ArrayList<TrungChatThai> listTrungShit = new ArrayList<TrungChatThai>();
	public GiaCam() {
	};

	public GiaCam(Texture texture, Vector2 pos, Vector2 direction) {
		super(texture, pos, direction);
	}

	@Override
	public void update() {
		float x = pos.x;// lay vi tri hien thoi cua gia cam
		// System.out.println(x);
		pos.add(direction);
		if (x >= GameConfig.WIDTH - this.texture.getWidth()) // gia cam chay het duong bien phai
			setPosition((-1)*GameConfig.GIA_CAM_SPEED, 0);
		if (x < 0)
			setPosition(GameConfig.GIA_CAM_SPEED, 0);

	}

	private TrungChatThai addTrungChatThai() { // de trung, sinh chat thai

		Random random = new Random();
		int ranNum = random.nextInt(10) + 1;
		if (ranNum % 2 == 0) {
			trungChatThai = new Trung();

		} else {

			trungChatThai = new Shit();
		}

		return trungChatThai;
	}

	public void deTrungShit(Vector2 vt, SpriteBatch sb) {
		if (!timerOn) {
			timerOn = true;

			Timer.schedule(new Task() { // tick timer

						@Override
						public void run() {
							GiaCam gc = new GiaCam();
							trungChatThai = gc.addTrungChatThai();
							timerOn = false;
						}

					}, GameConfig.TIME_SPAW);
			// /////////

			if (trungChatThai instanceof Trung) {				
				TrungChatThai trung = new Trung(vt, new Vector2(0, 0));
				listTrungShit.add(trung);
			}
			if (trungChatThai instanceof Shit) {
				TrungChatThai shit = new Shit(vt, new Vector2(0, 0));
				listTrungShit.add(shit);
			}

		}//end if timerOn
		
		if (!listTrungShit.isEmpty()) {
			for (int i = 0; i < listTrungShit.size(); i++) {
				listTrungShit.get(i).render(sb);
				listTrungShit.get(i).update();
			}
		}

	}
	public ArrayList<TrungChatThai> getListTrungChatThai(){
		return listTrungShit;
	}
	public void removeTrungChatThai(int index){
		listTrungShit.remove(index);
	}
	
	public TrungChatThai getTrungChatThai(int index){
		return listTrungShit.get(index);
	}
		
	
}
